<?php

namespace App\Admin\Controllers\Permission;

use App\Extends\BaseController;
use App\Requests\Permission\RoleRequest;
use App\Services\Permission\RoleService;
use Illuminate\Http\Request;

class RoleController extends BaseController
{
    public function __construct(RoleService $service)
    {
        parent::__construct($service);
        $this->service = $service;
    }

    /**
     * 显示资源列表
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(Request $request): \Illuminate\Http\JsonResponse
    {
        $where = [
            ['name', 'like', '%' . $request->get('name') . '%'],
            ['mark', 'like', '%' . $request->get('mark') . '%'],
        ];
        return $this->paginate($this->service->paginate($where));
    }

    /**
     * 保存新建资源
     * @param RoleRequest $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function store(RoleRequest $request): \Illuminate\Http\JsonResponse
    {
        return $this->success($this->service->create($request->all()));
    }

    /**+
     * 显示指定资源
     *
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function show($id): \Illuminate\Http\JsonResponse
    {
        return $this->success($this->service->find($id));
    }

    /**
     * 更新指定资源
     * @param RoleRequest $request
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function update(RoleRequest $request, $id): \Illuminate\Http\JsonResponse
    {
        return $this->success($this->service->update($id, $request->all()));
    }

    /**
     * 删除指定资源
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function destroy($id): \Illuminate\Http\JsonResponse
    {
        return $this->success($this->service->delete($id));
    }

    /**
     * 修改指定资源状态
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function setStatus($id): \Illuminate\Http\JsonResponse
    {
        return $this->success($this->service->setStatus($id));
    }

    /**
     * 获取角色列表
     * @return \Illuminate\Http\JsonResponse
     */
    public function getRoleAll()
    {
        return $this->success($this->service->all());
    }
}
